import { Log } from '../types';
import apiClient from './client';

// 获取日志列表
export const getLogs = async (params?: { page?: number; limit?: number; type?: string }): Promise<{ logs: Log[]; total: number }> => {
  const response = await apiClient.get('/logs/v1/', { params });
  // 提取 data 字段中的日志列表
  const logs = response.data.data.map((log: any) => ({
    ...log,
    user: log.user_id, // 将 user_id 映射为 user
  }));
  return {
    logs,
    total: logs.length, // 假设没有分页，直接返回日志总数
  };
};

// 获取单个日志
export const getLog = async (id: string): Promise<Log> => {
  const response = await apiClient.get(`/logs/${id}`);
  return response.data;
};

// 创建日志（通常由系统自动创建，但也可能需要手动创建）
export const createLog = async (logData: Omit<Log, 'id' | 'timestamp'>): Promise<Log> => {
  const response = await apiClient.post('/logs', logData);
  return response.data;
};

// 删除日志（如果需要）
export const deleteLog = async (id: string): Promise<void> => {
  await apiClient.delete(`/logs/${id}`);
};

// 导出日志接口
export const exportLogs = async (params?: { type?: string; user?: string; startDate?: string; endDate?: string }): Promise<Blob> => {
  const response = await apiClient.get('/logs/v1/export', { params, responseType: 'blob' });
  return response.data;
};
